package icu.makka.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.makka.yygh.cmn.client.DictFeignClient;
import icu.makka.yygh.enums.DictEnum;
import icu.makka.yygh.model.user.Patient;
import icu.makka.yygh.user.mapper.PatientMapper;
import icu.makka.yygh.user.mapper.UserInfoMapper;
import icu.makka.yygh.user.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 根据id获取就诊人信息
     * @param id
     * @return
     */
    @Override
    public Patient getPatientById(Long id) {
        log.info("业务层，根据id获取就诊人信息");
        return this.packPatient(baseMapper.selectById(id));
    }

    /**
     * 获取就诊人列表
     * @param userId
     * @return
     */
    @Override
    public List<Patient> findAllByUserId(Long userId) {
        log.info("业务层，获取就诊人列表");

        //根据userId查询所有就诊人信息
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null, Patient::getUserId, userId);
        List<Patient> patientList = baseMapper.selectList(queryWrapper);
        //封装其他参数
        patientList.stream().forEach(patient -> {
            this.packPatient(patient);
        });

        //返回结果
        return patientList;
    }

    /**
     * 封装Patient其他参数
     * @param patient
     */
    private Patient packPatient(Patient patient) {
        log.info("封装patient其他参数，patient={}", patient);

        //根据证件类型编码，获取证件类型具体所指
        String certificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),
                patient.getCertificatesType());
        //联系人证件类型
        String contactsCertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),
                patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        //封装参数
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());

        log.info("封装完毕，返回结果，patient={}", patient);
        return patient;
    }
}
